﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using Ssb.Logic;

namespace Ssb
{
    public class Stage
        : DrawableGameComponent
    {
        [ContentSerializerIgnore]
        public List<Fighter> Fighters; // Fighters are the people controlled by the player/computer

        public List<Bubble> ClingBubbles; // ClingBubbles are zones that the player may cling to
        public List<Rectangle> StageArea; // The stage areas are zones that the player may not pass through

        public Image BG; // The background is but a simple 1-frame image

        public Rectangle KillBounds; // Anything outside the killbounds dies

        private SpriteBatch sb;

        /// <summary>
        /// Main ctor for the Stage class.
        /// </summary>
        /// <param name="killBounds">The area outside of which fighters die</param>
        /// <param name="clingAreas">Areas onto which fighters may cling</param>
        /// <param name="stageZones">Areas that are unpassable</param>
        /// <param name="bg">The stage background</param>
        public Stage(Game game, Rectangle killBounds, List<Bubble> clingAreas, List<Rectangle> stageZones, Image bg)
            : base(game)
        {
            ClingBubbles = clingAreas;
            StageArea = stageZones;
            KillBounds = killBounds;
            BG = bg;
        }

        public override void Initialize()
        {

            base.Initialize();
        }

        /// <summary>
        /// InitializeStage creates a new list of fighters. If fighters are already present, they are deleted.
        /// </summary>
        /// <param name="fighters">The fighter list to use.</param>
        public void InitializeStage(List<Fighter> fighters)
        {
            if(Fighters.Count != 0)
                ResetStage();
            
            Fighters = fighters;
        }

        /// <summary>
        /// ResetStage deletes the list of fighters.
        /// </summary>
        public void ResetStage()
        {
            Fighters = new List<Fighter>();
        }

        /// <summary>
        /// Tests the stage's current fighters to see if they're outside the bounds
        /// </summary>
        /// <returns>The first occurrance of a dead fighter. Returns null if no fighters are dead.</returns>
        public Fighter areFightersInKillZone()
        {
            foreach (Fighter f in Fighters)
            {
                List<Vector2> corners = new List<Vector2>();
                corners.Add(new Vector2(f.Left, f.Top));
                corners.Add(new Vector2(f.Right, f.Top));
                corners.Add(new Vector2(f.Right, f.Bottom));
                corners.Add(new Vector2(f.Left, f.Bottom));

                foreach (Vector2 v in corners)
                {
                    if (v.X < KillBounds.Left || v.X > KillBounds.Right || v.Y < KillBounds.Bottom || v.Y > KillBounds.Top)
                        return f;
                }
            }

            return null;
        }

        public void Draw()
        {

        }

        public void Update()
        {

        }
    }
}
